package com.yanze.androidvideotask;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;


/**
 * 1. 在 Android 平台绘制一张图片，使用至少 3 种不同的 API，ImageView，SurfaceView，自定义 View
 */
public class Task1Activity extends AppCompatActivity {

    private ImageView iv_task1_1;
    private SurfaceView sv_task1_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task1);
        iv_task1_1 = findViewById(R.id.iv_task1_1);
        iv_task1_1.post(new Runnable() {
            @Override
            public void run() {
                iv_task1_1.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.xxb));
            }
        });
        sv_task1_2 = findViewById(R.id.sv_task1_2);
        sv_task1_2.post(new Runnable() {
            @Override
            public void run() {
                drawing();
            }
        });
    }

    private void drawing() {
        sv_task1_2.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if (surfaceHolder == null) {
                    return;
                }
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setStyle(Paint.Style.STROKE);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xxb);
                Canvas canvas = surfaceHolder.lockCanvas();
                canvas.drawBitmap(bitmap, 0, 0, paint);
                surfaceHolder.unlockCanvasAndPost(canvas);
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    /**
     * 展示图片
     */
    public static class ImageViewSV extends SurfaceView implements SurfaceHolder.Callback, Runnable{

        private SurfaceHolder mSurfaceHolder;
        //画布
        private Canvas mCanvas;
        //子线程标识位
        private boolean isDrawing;

        private Bitmap mBitmap;

        public ImageViewSV(Context context) {
            this(context,null);
        }

        public ImageViewSV(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public void init() {
            mSurfaceHolder = getHolder();//得到SurfaceHolder对象
            mSurfaceHolder.addCallback(this);//注册SurfaceHolder
            setFocusable(true);//能否获得焦点
            setFocusableInTouchMode(true);//能否通过触摸获得焦点
            this.setKeepScreenOn(true);//保持常亮

            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xxb);
        }

        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {//创建
            try {
                mCanvas = mSurfaceHolder.lockCanvas();
                onDraw(mCanvas);
            }finally {
                if (mCanvas != null) {
                    mSurfaceHolder.unlockCanvasAndPost(mCanvas);
                }
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {//改变
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {//销毁
            isDrawing = false;
        }

        @Override
        public void run() {
            while (isDrawing) {
                drawing();
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.YELLOW);
            canvas.drawBitmap(mBitmap,0,0,null);
        }

        private void drawing() {
        }

//        注意：
//        lockCanvas()获取到的Canvas对象还是上次的Canvas对象，并不是一个新的对象。之前的绘图都将被保留，如果需要擦除，可以在绘制之前通过drawColor()方法来进行清屏
    }

}
